unit uFrmPlayerOut;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, ComCtrls;

type
  TFrmPlayerOut = class(TForm)
    img_Player: TImage;
    lbl_PlayerName: TLabel;
    btn_Ok: TButton;
    btn_Cancel: TButton;
    dtp_DateStart: TDateTimePicker;
    Label1: TLabel;
    dtp_TimeStart: TDateTimePicker;
    edt_WinPos: TLabeledEdit;
    chb_EditValues: TCheckBox;
    procedure FormShow(Sender: TObject);
    procedure btn_OkClick(Sender: TObject);
    procedure btn_CancelClick(Sender: TObject);
    procedure chb_EditValuesClick(Sender: TObject);
  private
    { Private declarations }
    m_TournamentID: string;
  public
    { Public declarations }
  end;

var
  FrmPlayerOut: TFrmPlayerOut;

implementation

uses ufrmCageTournament, uFrmMain, umainutils;
{$R *.dfm}

procedure TFrmPlayerOut.btn_CancelClick(Sender: TObject);
begin
  close();
end;

procedure TFrmPlayerOut.btn_OkClick(Sender: TObject);
var
  DateTimeStart: string;
begin
  DateTimeStart := FormatDateTime('dd.mm.yyyy', dtp_DateStart.Date);
  DateTimeStart := DateTimeStart + ' ' + FormatDateTime('hh:nn:ss', dtp_TimeStart.Time);
  DateTimeStart := 'to_date(''' + DateTimeStart + ''', ''dd.mm.yyyy hh24:mi:ss'')';
  frmMain.MainUtils.TournamentPlayerFromTable(frmCageTournament.stg_Players.Cells[0, frmCageTournament.stg_Players.Row], m_TournamentID, DateTimeStart, edt_WinPos.Text);
end;

procedure TFrmPlayerOut.chb_EditValuesClick(Sender: TObject);
begin
  edt_WinPos.Enabled := chb_EditValues.Checked;
  dtp_DateStart.Enabled := chb_EditValues.Checked;
  dtp_TimeStart.Enabled := chb_EditValues.Checked;
end;

procedure TFrmPlayerOut.FormShow(Sender: TObject);
begin
  m_TournamentID := frmCageTournament.stg_Tournaments.Cells[0, frmCageTournament.stg_Tournaments.Row];
  img_Player.Picture := frmCageTournament.img_Player.Picture;
  lbl_PlayerName.Caption := frmCageTournament.stg_Players.Cells[1, frmCageTournament.stg_Players.Row];
  frmMain.OracleQuery1.close;
  frmMain.OracleQuery1.SQL.Text := 'SELECT SYSDATE FROM DUAL';
  frmMain.OracleQuery1.Execute;
  dtp_DateStart.DateTime := frmMain.OracleQuery1.Field(0);
  dtp_TimeStart.DateTime := frmMain.OracleQuery1.Field(0);
  chb_EditValues.Checked := false;
  OraQueryMainUtils.SQL.Clear;
  OraQueryMainUtils.SQL.Text := ('SELECT (TICKET - (SELECT SUM(CASE WHEN OUT_DATETIME<(select sysdate from dual)THEN COUNT(1) ELSE 0 END) AS POSITIONS FROM TICKETS WHERE TOURNAMENT_ID=' +
      m_TournamentID + ' AND DELETED=0 GROUP BY OUT_DATETIME)) AS POSITION FROM(' + ' SELECT COUNT(PLAYER_ID) AS TICKET  FROM TICKETS TI WHERE TOURNAMENT_ID=' + m_TournamentID + ' AND DELETED=0)');
  OraQueryMainUtils.Execute;
  edt_WinPos.Text := OraQueryMainUtils.Field(0);
end;

end.
